package com.wechat.DAO.DaoImpl;

import com.wechat.DAO.UserInfoDAO;
import com.wechat.pojo.User;
import com.wechat.pojo.UserInfo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.*;

public class UserInfoDAOIpml implements UserInfoDAO {
    Connection connection = null;
    PreparedStatement pst = null;
    ResultSet resultSet = null;

    @Override
    public String add(String username, String nickname, String sex, String country, String province, String city, String HPUrl) {
        try {
            getConn();
            //提交表单
            pst = connection.prepareStatement("insert into USER_INFO values (?,?,?,?,?,?,?)");
            pst.setString(1,username);
            pst.setString(2,nickname);
            pst.setString(3,sex);
            pst.setString(4,country);
            pst.setString(5,province);
            pst.setString(6,city);
            pst.setString(7,HPUrl);
            pst.execute();
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }finally {
            closeAll();
        }
        return "success";
    }

    @Override
    public String update(String username, String nickname, String sex, String country, String province, String city, String HPUrl) {
        UserInfo userInfo = null;
        //防止SQL注入
            try {
                getConn();
                PreparedStatement pst = connection.prepareStatement("UPDATE USER_INFO SET NICKNAME=?,SEX=?,COUNTRY=?,PROVINCE=?,CITY=?,HPURL=? WHERE USERNAME=?");
                pst.setString(1,nickname);
                pst.setString(2,sex);
                pst.setString(3,country);
                pst.setString(4,province);
                pst.setString(5,city);
                pst.setString(6,HPUrl);
                pst.execute();
            } catch (Exception e) {
                e.printStackTrace();
                return "fail";
            }finally {
                closeAll();
            }
            return "success";
    }

    @Override
    public UserInfo query(String username) {
        UserInfo userInfo = null;
        try {
            System.out.println(username);
//            getConn();
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/wechat?useSSL=false", "root", "123456");
            PreparedStatement pst = connection.prepareStatement("SELECT * FROM USER_INFO WHERE USERNAME=?");
            pst.setString(1,username);
            resultSet = pst.executeQuery();
            while (resultSet.next()){
                userInfo = new UserInfo();
                if(resultSet.getString(1)==null || resultSet.getString(1).isEmpty()){
                    return null;
                }
                userInfo.setUsername(resultSet.getString(1));
                userInfo.setNickname(resultSet.getString(2));
                userInfo.setSex(resultSet.getString(3));
                System.out.println("查询中，准备查找地区信息"); //因为可能为空，防止报错找不到地方
                userInfo.setCountry(resultSet.getString(4));
                userInfo.setProvince(resultSet.getString(5));
                userInfo.setCity(resultSet.getString(6));
                System.out.println("查询中，查找地区信息成功");
                userInfo.setHPUrl(resultSet.getString(7));
            }
            System.out.println("查询到一个结果");
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            if(resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            closeAll();
        }
        return userInfo;
    }

    @Override
    public Boolean updateNickname(String value, String username) {
        //防止SQL注入
        try {
            getConn();
            PreparedStatement pst = connection.prepareStatement("UPDATE USER_INFO SET NICKNAME=? WHERE USERNAME=?");
            pst.setString(1,value);
            pst.setString(2,username);
            pst.execute();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            closeAll();
        }
        return true;
    }

    @Override
    public Boolean updateHPUrl(String value, String username) {
        //防止SQL注入
        try {
            getConn();
            PreparedStatement pst = connection.prepareStatement("UPDATE USER_INFO SET HPUrl=? WHERE USERNAME=?");
            pst.setString(1,value);
            pst.setString(2,username);
            pst.execute();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            closeAll();
        }
        return true;
    }

    @Override
    public Boolean updateCountry(String value, String username) {
        //防止SQL注入
        try {
            getConn();
            PreparedStatement pst = connection.prepareStatement("UPDATE USER_INFO SET Country=? WHERE USERNAME=?");
            pst.setString(1,value);
            pst.setString(2,username);
            pst.execute();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            closeAll();
        }
        return true;
    }

    @Override
    public Boolean updateProvince(String value, String username) {
        //防止SQL注入
        try {
            getConn();
            PreparedStatement pst = connection.prepareStatement("UPDATE USER_INFO SET Province=? WHERE USERNAME=?");
            pst.setString(1,value);
            pst.setString(2,username);
            pst.execute();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            closeAll();
        }
        return true;
    }

    @Override
    public Boolean updateCity(String value, String username) {
        //防止SQL注入
        try {
            getConn();
            PreparedStatement pst = connection.prepareStatement("UPDATE USER_INFO SET City=? WHERE USERNAME=?");
            pst.setString(1,value);
            pst.setString(2,username);
            pst.execute();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            closeAll();
        }
        return true;
    }

    @Override
    public Boolean updateSex(String value, String username) {
        //防止SQL注入
        try {
            getConn();
            PreparedStatement pst = connection.prepareStatement("UPDATE USER_INFO SET Sex=? WHERE USERNAME=?");
            pst.setString(1,value);
            pst.setString(2,username);
            pst.execute();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            closeAll();
        }
        return true;
    }


    private void getConn(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection("jdbc:mysql:///wechat?useSSL=false", "root", "123456");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void closeAll() {
        if(pst!=null){
            try {
                pst.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
